/*
 * Copyright (C) 2017-2021
 * All rights reserved, Designed By 深圳中科鑫智科技有限公司
 * Copyright authorization contact 18814114118
 */
package com.shop.cereshop.app.service.recommend.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.cereshop.app.dao.buyer.CereBuyerUserDAO;
import com.shop.cereshop.app.dao.recommend.RecommendAtUserDAO;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.domain.buyer.FollowUserListVo;
import com.shop.cereshop.commons.domain.buyer.FollowUserVo;
import com.shop.cereshop.commons.domain.im.tx.req.follow.FollowGetReq;
import com.shop.cereshop.commons.domain.im.tx.req.friend.FriendItem;
import com.shop.cereshop.commons.domain.im.tx.req.friend.GetFriendListReq;
import com.shop.cereshop.commons.domain.im.tx.resp.follow.FollowGetResp;
import com.shop.cereshop.commons.domain.im.tx.resp.friend.FriendBaseResp;
import com.shop.cereshop.commons.domain.im.tx.resp.friend.FriendUserDataItem;
import com.shop.cereshop.commons.domain.recommend.RecommendAtUser;
import com.shop.cereshop.app.service.recommend.RecommendAtUserService;

import com.shop.cereshop.commons.result.Result;
import com.shop.cereshop.commons.service.im.ImUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 *
 * @author 罗霄
 * @用户表 </p>
 * @date 2025-08-25
 */
@Slf4j
@Service
public class RecommendAtUserServiceImpl extends ServiceImpl<RecommendAtUserDAO, RecommendAtUser> implements RecommendAtUserService {
    @Resource
    private RecommendAtUserDAO recommendAtUserDAO;
    @Resource
    private ImUserService imUserService;
    @Autowired
    private CereBuyerUserDAO cereBuyerUserDAO;


    @Override
    public Page<FollowUserVo> getRecentAtUserList(Integer pageNum, Integer pageSize, Integer type, String name, Long buyerUserId) {
        List<String> userIdList = null;
        Page page = new Page<>(pageNum, pageSize);
        switch (type) {
            case 3: {// 好友
                GetFriendListReq req = new GetFriendListReq();
                req.setFromAccount(String.valueOf(buyerUserId));
                req.setStartIndex((pageNum - 1));
//                req.setStandardSequence();
                FriendBaseResp<FriendItem> friendBaseResp = imUserService.getFriendList(req);
                System.out.println(friendBaseResp);
                //提取用户id
                if (StrUtil.equals("OK", friendBaseResp.getActionStatus())) {
                    if (CollUtil.isNotEmpty(friendBaseResp.getUserDataItem())) {
                        //提取 friendBaseResp.getUserDataItem() 中的 To_Account 生成一个集合
                        userIdList = friendBaseResp.getUserDataItem().stream().map(FriendUserDataItem::getTo_Account).collect(Collectors.toList());
                    }
                }
                break;
            }
            case 2: // 关注
                FollowGetReq req = new FollowGetReq();
                req.setFrom_Account(String.valueOf(buyerUserId));
                //1 - 粉丝
                //2 - 关注
                //3 - 互关
                req.setFollowType(type);
                int pages = (pageNum - 1) * pageSize;
                // 将页码偏移量格式化为10位数字字符串，不足位数用0填充，例如：页码为1时，pages=0，格式化后为"0000000000"；页码为2时，pages=10，格式化后为"0000000010"
                String startCursor = String.format("%016d", pages);
                req.setStartCursor(pageNum == 1 ? "" : startCursor);
                req.setWantNum(pageSize);
                FollowGetResp followGetResp = imUserService.getFollowList(req);
                System.out.println(followGetResp);
                if (StrUtil.equals("OK", followGetResp.getActionStatus())) {
                    if (CollUtil.isNotEmpty(followGetResp.getFollowItem())) {
                        //提取关注列表accountUserId
                        userIdList = followGetResp.getFollowItem().stream().map(FollowGetResp.FollowGet::getTo_Account).collect(Collectors.toList());
                        break;
                    }
                }
            default: // 最近@
                IPage<RecommendAtUser> recommendAtUserIPage = recommendAtUserDAO.selectPage(page,
                        new QueryWrapper<RecommendAtUser>().eq("buyer_user_id", buyerUserId).orderByDesc("create_time"));
                if (CollUtil.isNotEmpty(recommendAtUserIPage.getRecords())) {
                    userIdList = recommendAtUserIPage.getRecords().stream().map(recommendAtUser -> String.valueOf(recommendAtUser.getAtUserId())).collect(Collectors.toList());
                }
        }
        if (CollUtil.isEmpty(userIdList)) {
            return page;
        }
        List<CereBuyerUser> CereBuyerUserList = cereBuyerUserDAO.selectByBuyerUserIds(userIdList, name);
        List<FollowUserVo> followUserList = new ArrayList<>();
        for (CereBuyerUser cereBuyerUser : CereBuyerUserList) {
            FollowUserVo followUserVo = new FollowUserVo();
            followUserVo.setUserId(cereBuyerUser.getBuyerUserId());
            followUserVo.setName(cereBuyerUser.getName());
            followUserVo.setHeadImage(cereBuyerUser.getHeadImage());
            followUserList.add(followUserVo);
        }
        page.setRecords(followUserList);
        return page;
    }
}
